package cn.hotel.controller.worker;

import cn.hotel.entity.Order;
import cn.hotel.response.ResponseTool;
import cn.hotel.response.Result;
import cn.hotel.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hotel.entity.Order;
import cn.hotel.service.RoomTypeService;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 订单接口
 */
@Slf4j
@RestController
@RequestMapping(value = "/op/order")
public class OpOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RoomTypeService roomTypeService;

    @PostMapping("/count")
    public Result selectOrdersCount(){
        int totalOrdersCount = orderService.getOrders();
        return ResponseTool.success(totalOrdersCount);
    }
    /**
     * 遍历所有订单信息
     * @return
     */
    @PostMapping
    public Result allOrder(String params,Integer status) {
//        List<Order> orders = orderService.allOrder();
        log.info("roomType:{}",params);
        List<Order> orders = orderService.allOrder(params , status);
        log.info("order信息{}" , orders);
        if (orders == null) {
            return ResponseTool.success("查询失败");
        }
        return ResponseTool.success(orders);
    }


    /**
     * 根据返回回来的orderId来删除订单信息
     * @param orderId
     * @return
     */
    @PostMapping("/delete")
    public Result deleteByOrderId(Integer orderId){
        log.info("获取的orderId为{}" , orderId);
        int i = orderService.deleteByOrderId(orderId);
        if (i == 0) {
            return ResponseTool.failed("删除失败");
        }
        return ResponseTool.success();
    }

    /**
     * 批量删除操作
     * @param orders
     * @return
     */
    @PostMapping("/deleteS")
    public Result delOrders(@RequestParam Integer[] orders) {
        log.info("获取的orders的信息{} ");
        if (orders == null) {
            return ResponseTool.failed("查询失败!!!");
        }

        for (int i = 0; i < orders.length; i++) {
            orderService.deleteByOrderId(orders[i]);
        }
        return ResponseTool.success();
    }


    @PostMapping("/add")
    public Result addOrder(Order order){
        log.info("获取的order的信息{}" , order);
        int i = orderService.addOrder(order);
        int i1 = roomTypeService.updateRoomRest(order.getRoomTypeId(), "-1");

        if (i == 0 && i1 == 0 ) {
            return ResponseTool.failed("添加失败!!!");
        }
        return ResponseTool.success();
    }


    @PostMapping("/{orderId}")
    public Result queryById(@PathVariable String orderId) {
        log.info("获取的orderId的值{}" , orderId);
        Order orderById = orderService.getOrderById(orderId);
        return ResponseTool.success(orderById);
    }

    @PostMapping("/update")
    public Result updateOrder(Order order) {
        log.info("获取的order的值{}" , order);
        int i = orderService.updateOrder(order);
        return ResponseTool.success();
    }

    @PostMapping("/byName/{name}")
    public Result getOrderByName(@PathVariable String name){
        log.info("获取的name:{}" , name);
        List<Order> orderByName = orderService.getOrderByName(name);

        if (orderByName == null) {
            return ResponseTool.failed("查询失败");
        }
        return ResponseTool.success(orderByName);
    }

    @PostMapping("/withNameAndPhone")
    public Result withNameAndPhone(Order order){
        String name = order.getName();
        String phone = order.getPhone();

        Order orderByNameAndPhone = orderService.getOrderByNameAndPhone(name , phone);

        if (orderByNameAndPhone == null) {
            ResponseTool.failed("查询失败");
        }

        return ResponseTool.success(orderByNameAndPhone);
    }
}
